package za.ac.wits.was.controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import za.ac.wits.was.courses.Course;
import za.ac.wits.was.courses.allocations.CourseAllocations;
import za.ac.wits.was.courses.allocations.CourseAllocationsService;
import za.ac.wits.was.disciplines.Discipline;
import za.ac.wits.was.disciplines.DisciplineService;
import za.ac.wits.was.research.ResearchAllocations;
import za.ac.wits.was.research.ResearchAllocationsService;
import za.ac.wits.was.user.model.AcademicStaff;

/**
 *
 * @author Salman Noor : School of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email: salmannoor74415@gmail.com
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 */
@Controller
@RequestMapping("/approvals/*")
public class ApprovalsController {

    Logger L = Logger.getLogger(ApprovalsController.class);
    @Autowired
    private ResearchAllocationsService researchAllocationsService;
    @Autowired
    private CourseAllocationsService courseAllocationsService;
    @Autowired
    private DisciplineService disciplineService;
    
    
            
    @RequestMapping(value = "/Allocate", method = RequestMethod.GET)
    public String AllocateCourse(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "AllocateCourse");
        return "allocatecourse";
    }
    
        @RequestMapping(value = "/Allocate/staffmembers", method = RequestMethod.GET)
    public @ResponseBody
    String getStaffMembers(final @RequestParam(value = "discipline_id") Long discipline_id) throws IOException {
            List<AcademicStaff> as = this.disciplineService.getStaffMembers(discipline_id);
        L.debug("Staff List is :" + as.toString());
        return new ObjectMapper().writeValueAsString(as);
    }
        
        
    @RequestMapping(value = "/Allocate/courses", method = RequestMethod.GET)
    public @ResponseBody
    String getCourses(final @RequestParam(value = "discipline_id") Long discipline_id) throws IOException {
            List<Course> cs = this.disciplineService.getCourses(discipline_id);
        L.debug("Course List is :" + cs.toString());
        return new ObjectMapper().writeValueAsString(cs);
    }
    
            @RequestMapping(value = "/Allocate/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewCourseAllocation(final HttpServletRequest httpServletRequest,
    @RequestParam("academic_staff_id") Long academic_staff_id,
    @RequestParam("course_id") Long course_id,
    @RequestParam("start_date") @DateTimeFormat(pattern="yyyy-MM-dd")  Calendar start_date) throws IOException {
         
         CourseAllocations newCourseAllocations = this.courseAllocationsService.createApprovedNewCourseAllocations(course_id, academic_staff_id, start_date);
            System.out.println(course_id.toString() +" "+start_date.toString());
            return new ObjectMapper().writeValueAsString(newCourseAllocations);
    } 
    
    
        
                @RequestMapping(value = "/Allocations", method = RequestMethod.GET)
    public String allAllocations(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "AllAllocations");
        return "allallocations";
    }
                
    @RequestMapping(value = "/Allocations/disciplines", method = RequestMethod.POST)
    public @ResponseBody
    String readDisciplines() throws IOException {

        List<Discipline> ds = this.disciplineService.getAllDisciplines();
        L.debug("Discipline List is :" + ds.toString());
        return new ObjectMapper().writeValueAsString(ds);
    }
    
        @RequestMapping(value = "/Allocations/research/{discipline_id}", method = RequestMethod.GET)
    public @ResponseBody
    String getActiveResearch(final @PathVariable(value = "discipline_id") Long discipline_id) throws IOException {
            List<ResearchAllocations> ra = this.researchAllocationsService.getActiveResearchAllocations(discipline_id);
        L.debug("Research List is :" + ra.toString());
        return new ObjectMapper().writeValueAsString(ra);
    }
        
    @RequestMapping(value = "/Allocations/courses/{discipline_id}", method = RequestMethod.GET)
    public @ResponseBody
    String getActiveCourses(final @PathVariable(value = "discipline_id") Long discipline_id) throws IOException {
            List<CourseAllocations> ca = this.courseAllocationsService.getActiveCourseAllocations(discipline_id);
        L.debug("Course List is :" + ca.toString());
        return new ObjectMapper().writeValueAsString(ca);
    }
        
            
    @RequestMapping(value = "/Research", method = RequestMethod.GET)
    public String research(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "ResearchApprovals");
        return "researchapprovals";
    }

    @RequestMapping(value = "/Courses", method = RequestMethod.GET)
    public String courses(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "CourseApprovals");
        return "courseapprovals";
    }

    @RequestMapping(value = "/Courses/read", method = RequestMethod.GET)
    public @ResponseBody
    String getAllPendingCourseTeachingRequests(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) throws IOException {
        List<CourseAllocations> pendingCourseAllocations = this.courseAllocationsService.getPendingCourseAllocations();
        L.debug("Pending Course Approval List is :" + pendingCourseAllocations.toString());
        return new ObjectMapper().writeValueAsString(pendingCourseAllocations);
    }

    @RequestMapping(value = "/Courses/approve", method = RequestMethod.POST)
    public @ResponseBody
    String approvePendingCourseAllocation(final HttpServletRequest httpServletRequest,
            @RequestParam("course_allocation_id") Long course_allocation_id) throws IOException {
        CourseAllocations ca = this.courseAllocationsService.approveCourseAllocation(course_allocation_id);
        return new ObjectMapper().writeValueAsString(ca);
    }

    @RequestMapping(value = "/Courses/decline", method = RequestMethod.POST)
    public @ResponseBody
    String declinePendingCourseAllocation(final HttpServletRequest httpServletRequest,
            @RequestParam("course_allocation_id") Long course_allocation_id) throws IOException {
        CourseAllocations ca = this.courseAllocationsService.declineCourseAllocation(course_allocation_id);
        return new ObjectMapper().writeValueAsString(ca);
    }

    @RequestMapping(value = "/Research/read", method = RequestMethod.GET)
    public @ResponseBody
    String getAllPendingResearchRequests(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) throws IOException {
        List<ResearchAllocations> pendingResearchAllocations = this.researchAllocationsService.getPendingResearchAllocations();
        L.debug("Pending Research Approval List is :" + pendingResearchAllocations.toString());
        return new ObjectMapper().writeValueAsString(pendingResearchAllocations);
    }

    @RequestMapping(value = "/Research/approve", method = RequestMethod.POST)
    public @ResponseBody
    String approvePendingResearchAllocation(final HttpServletRequest httpServletRequest,
            @RequestParam("research_allocation_id") Long research_allocation_id) throws IOException {
        ResearchAllocations ra = this.researchAllocationsService.approveResearch(research_allocation_id);
        return new ObjectMapper().writeValueAsString(ra);
    }

    @RequestMapping(value = "/Research/decline", method = RequestMethod.POST)
    public @ResponseBody
    String declinePendingResearchAllocation(final HttpServletRequest httpServletRequest,
            @RequestParam("research_allocation_id") Long research_allocation_id) throws IOException {
        ResearchAllocations ra = this.researchAllocationsService.declineResearch(research_allocation_id);
        return new ObjectMapper().writeValueAsString(ra);
    }
}
